//
//  XMLHomeCellViewModel.swift
//  Weibo
//
//  Created by mac on 16/5/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class XMLHomeCellViewModel: NSObject {
    
    var home :XMLHome?
    
    var retweetContent :String?
    
    var retweetCountContent :String?
    var commentCountContent :String?
    var unlikeCountContent :String?
    
    var timecontent :String? {
    
        guard let creatAt = home?.created_at else {
        
            return nil
        }
        return NSDate.sinaDate(creatAt).sinaDateString
        //  代码执行到此createAt 一定有值 Mon May 16 10:39:03 +0800 2016

//        let dateFormatter = NSDateFormatter()
//        dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
//        dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
//        let creatAtDate = dateFormatter.dateFromString(creatAt)!
////        print(creatAtDate)
//        /*
//        判断是否是今年
//            判断是否是今天
//                判断是否是1分钟以内
//                    刚刚
//                判断是否是1小时之前
//                    xx分钟前
//                其它
//                    xx小时前
//            判断是否是昨天
//                昨天 09:02
//            其它
//                05-06 10:02
//        不是今年
//            2015-10-10 10:10
//        
//        */
//        
//        if isThisYear(creatAtDate) {
//            let calendar = NSCalendar.currentCalendar()
//            if calendar.isDateInToday(creatAtDate) {
//                let timeIntVal = abs(creatAtDate.timeIntervalSinceNow)
//                if timeIntVal < 60 {
//                
//                    return "刚刚"
//                }else if timeIntVal < 3600 {
//                
//                    return "\(Int(timeIntVal / 60))分钟前"
//                }else {
//                
//                    return "\(Int(timeIntVal / 3600))小时前"
//                }
//                
//            }else if calendar.isDateInYesterday(creatAtDate) {
//            
//                dateFormatter.dateFormat = "昨天 HH:mm"
//            }else {
//            
//                dateFormatter.dateFormat = "MM-dd HH:mm"
//            }
//        }else {
//        
//            dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
//            
//        }
//        
//        return dateFormatter.stringFromDate(creatAtDate)
    }
    
    var sourceContent :String?
    var mbrankImage :UIImage?
    var verifiedImage :UIImage?
    
    init(home :XMLHome) {
        super.init()
        self.home = home
        handleRetweetContent(home)
        
        retweetCountContent = handleCountContent(11002, defaultTitle: "转发")
        commentCountContent = handleCountContent(home.comments_count, defaultTitle: "评论")
        unlikeCountContent = handleCountContent(home.attitudes_count, defaultTitle:"赞")
        handleSourceContent(home)
//        print(timecontent)
        handleMbrankIMage(home.user?.mbrank ?? 0)
        
        handleVerifiedImage(home.user?.verified_type ?? 0)
        
    }
    
//    private func isThisYear(creatAtDate :NSDate) -> Bool {
//    
//        let currentDate = NSDate()
//        let dateFormatter = NSDateFormatter()
//        dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
//        dateFormatter.dateFormat = "yyyy"
//        
//        let currentYear = dateFormatter.stringFromDate(currentDate)
//        let creatAtYear = dateFormatter.stringFromDate(creatAtDate)
//        
//        return currentYear == creatAtYear
//    }
    
    private func handleVerifiedImage(verifiedType :Int) {
        
    switch verifiedType {
                //  认证类型 -1 没有认证 ，0 认证用户，2，3，5 企业认证 ， 220 达人
        case 0:
            verifiedImage = UIImage(named: "avatar_vip")
        case 2,3,5:
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedImage = nil
        
        
        
        }
        
    }
    private func handleMbrankIMage(mbrank :Int) {
    
        mbrankImage = UIImage(named: "common_icon_membership_level\(mbrank)")
    }

    private func handleCountContent(count :Int, defaultTitle :String)->String {
    
        if count > 0 {
            if count >= 10000 {
                let result = CGFloat(count / 1000 / 10)
                let resultStr = "\(result)"
                if resultStr.containsString(".0") {
                    return resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                return resultStr
                
                
            }else {
            
                return "\(count)"
            }
         
            
        }else {
        
            return defaultTitle
        }
    }
    
    private func handleRetweetContent(home :XMLHome) {
    
        guard let retweetText = home.retweeted_status?.text else {
        
            return
        }
        guard let name = home.retweeted_status?.user?.screen_name else {
        
            return
        }
        
        retweetContent = "@\(name) : \(retweetText)"
        
    }
    
    private func handleSourceContent(home: XMLHome) {
        //  判断是否存在
        guard let source = home.source else {
            return
        }
        
        //  代码执行到此,来源数据一定存在
        //  '\'转译符号
        
        if source.containsString("\">") {
            let startIndex = source.rangeOfString("\">")?.endIndex
            let endIndex = source.rangeOfString("</")?.startIndex
            
            if let start = startIndex, end = endIndex {
                sourceContent = source.substringWithRange(start..<end)
            }
            
        }
        
    }
}
